// ActionScript file
import enums.HotKeys;

import flash.events.KeyboardEvent;

import model.Chapter;

import mx.controls.textClasses.TextRange;
import mx.core.Application;

[Bindable]
public var chapter:Chapter;

private function init():void{
	this.titleTextField.selectable = true;	
}

private function markDirty():void{
	Application.application.enableSaveOption();
	chapter.isDirty = true;
	this.marker.text = "*"
}

public function markClean():void{
	Application.application.disableSaveOption();
	chapter.isDirty = false;
	this.marker.text = "";
}

private function ignoreTabs(event:KeyboardEvent):void{
	if (event.keyCode == HotKeys.TAB){
		event.preventDefault();
		insertTab();		
	}	
}

public function save():void{
	chapter.title = title;
	chapter.contents = textEditor.textArea.htmlText;	
	Application.application.saveChapter(chapter);
	markClean();
}

private function insertTab():void{
	 var beginIndex:int = this.textEditor.textArea.selectionBeginIndex;
     var range:TextRange = new TextRange(this.textEditor.textArea, false, beginIndex, beginIndex);
     range.text = "\t";
     var nextIndex:int = this.textEditor.textArea.selectionBeginIndex+range.text.length;
     this.textEditor.textArea.setSelection(nextIndex, nextIndex);
}
